import unittest
from flask import Flask
from web.device_controller import device_blueprint
from web.slave_controller import slave_blueprint


class TestAddDevice(unittest.TestCase):
    def setUp(self):
        self.app = Flask(__name__)
        self.app.register_blueprint(device_blueprint)
        self.app.register_blueprint(slave_blueprint)
        self.client = self.app.test_client()

    def test_add_device_success(self):
        response = self.client.post('/device/add_device', json={
            'device_name': 'Device1',
            'ip': '192.168.1.1',
            'port': 8080,
            'protocol': 'TCP',
            'slave_cnt': 1
        })
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json['msg'], '添加设备成功!')

    def test_get_device_name_list(self):
        response = self.client.get('/device/get_device_name_list')
        print(response.data)

    def test_write_register(self):
        response = self.client.post('/slave/write_register', json={
            'device_name': '测试',
            'slave_id': 1,
            'register_type': 3,
            'start_address': 0,
            'data_type': "SignedShort",
            'value_list': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        })
        print(response.data)


if __name__ == '__main__':
    unittest.main()
